package cn.com.nes.site.action.crm;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.crm.OpportunityService;

@Controller("opportunity")
@RequestMapping("/opportunity")
public class OpportunityAction {
    @Resource
    private OpportunityService service;

    //获取商机列表
    @RequestMapping(method = RequestMethod.POST, value = "/getOpportunityList")
    public @ResponseBody
    PageData getOpportunityList(HttpServletRequest request,
                      HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getOpportunityList(req) ;
        return res;
    }

    //获取商机明细
    @RequestMapping(method = RequestMethod.POST, value = "/getOpportunityDetail")
    public @ResponseBody
    PageData getOpportunityDetail(HttpServletRequest request,
                                HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getOpportunityDetail(req) ;
        return res;
    }

    //添加商机
    @RequestMapping(method = RequestMethod.POST, value = "/saveOpportunity")
    public @ResponseBody  PageData saveOpportunity(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.saveOpportunity(req);
        return res;
    }
    
    //删除商机
    @RequestMapping(method = RequestMethod.POST, value = "/delOpportunity")
    public @ResponseBody  PageData delOpportunity(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.delOpportunity(req) ;
        return res;
    }
    
  //商机流失
    @RequestMapping(method = RequestMethod.POST, value = "/opportunityLoss")
    public @ResponseBody  PageData opportunityLoss(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.opportunityLoss(req) ;
        return res;
    }
    

    
  //获取商机动态
//    @RequestMapping(method = RequestMethod.POST, value = "/getOpportunityDynamic")
//    public @ResponseBody  PageData getOpportunityDynamic(HttpServletRequest request,
//                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
//        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
//        PageData res = new PageData() ;
//        if (req_Status.getRetCode() != 0) {
//            res = new PageData();
//            res.setRetCode(req_Status.getRetCode()) ;
//            res.setRetMessage(req_Status.getRetMessage());
//            return res;
//        }
//        DataReq req = req_Status.getReq() ;
//        res = service.getOpportunityDynamic(req) ;
//        return res;
//    }
    
    //添加跟进记录
    @RequestMapping(method = RequestMethod.POST, value = "/addOpportunityClaim")
    public @ResponseBody  PageData saveOpportunityClaim(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.saveOpportunityClaim(req) ;
        return res;
    }
    
    //删除跟进记录
    @RequestMapping(method = RequestMethod.POST, value = "/delOpportunityClaim")
    public @ResponseBody  PageData delOpportunityClaim(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.delOpportunityClaim(req) ;
        return res;
    }
    
    //添加商机联系人
    @RequestMapping(method = RequestMethod.POST, value = "/addOpportunityStaff")
    public @ResponseBody  PageData saveOpportunityStaff(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.saveOpportunityStaff(req) ;
        return res;
    }
    
  //删除商机联系人
    @RequestMapping(method = RequestMethod.POST, value = "/delOpportunityStaff")
    public @ResponseBody  PageData delOpportunityStaff(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.delOpportunityStaff(req) ;
        return res;
    }
    
    //添加商机文件
    @RequestMapping(method = RequestMethod.POST, value = "/saveOpportunityFile")
    public @ResponseBody
    PageData saveOpportunityFile(HttpServletRequest request,
                      HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.addOpportunityFile(req) ;
        return res;
    }
    
  //删除商机文件
    @RequestMapping(method = RequestMethod.POST, value = "/delOpportunityFile")
    public @ResponseBody
    PageData delOpportunityFile(HttpServletRequest request,
                      HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.delOpportunityFile(req) ;
        return res;
    }
    
    //添加报价单
    @RequestMapping(method = RequestMethod.POST, value = "/addOpportunityQuote")
    public @ResponseBody  PageData addOpportunityQuote(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.addOpportunityQuote(req) ;
        return res;
    }
    
  //删除报价单
    @RequestMapping(method = RequestMethod.POST, value = "/delOpportunityQuote")
    public @ResponseBody  PageData delOpportunityQuote(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.delOpportunityQuote(req) ;
        return res;
    }
    
    //查看报价单详情
    @RequestMapping(method = RequestMethod.POST, value = "/getQuoteDetail")
    public @ResponseBody  PageData getQuoteDetail(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getQuoteDetail(req) ;
        return res;
    }
    
  //获取设计图列表
    @RequestMapping(method = RequestMethod.POST, value = "/getBimList")
    public @ResponseBody  PageData getBimList(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getBimList(req) ;
        return res;
    }
    
    //删除设计图
    @RequestMapping(method = RequestMethod.POST, value = "/delBim")
    public @ResponseBody  PageData delBim(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.delBim(req) ;
        return res;
    }
    
    //商机删除设计图
    @RequestMapping(method = RequestMethod.POST, value = "/delOpportunityBim")
    public @ResponseBody  PageData delOpportunityBim(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.delOpportunityBim(req) ;
        return res;
    }
    
  //商机添加设计图
    @RequestMapping(method = RequestMethod.POST, value = "/addOpportunityBim")
    public @ResponseBody  PageData addOpportunityBim(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.addOpportunityBim(req) ;
        return res;
    }
    
  //转移商机
    @RequestMapping(method = RequestMethod.POST, value = "/transferOpportunity")
    public @ResponseBody  PageData transferOpportunity(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.transferOpportunity(req) ;
        return res;
    }
    
    //更新bim数据
    @RequestMapping(method = RequestMethod.POST, value = "/updateBimList")
    public @ResponseBody  PageData updateBimList(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        service.updateBimList() ;
        res.setRetCode(0);
        res.setRetMessage("OK");
        return res;
    }
    
    //更新设计数据
    @RequestMapping(method = RequestMethod.POST, value = "/updateDesignDetail")
    public @ResponseBody  PageData updateDesignDetail(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        service.updateDesignDetail(req) ;
        res.setRetCode(0);
        res.setRetMessage("OK");
        return res;
    }
    
  //获取清单列表
    @RequestMapping(method = RequestMethod.POST, value = "/getDesignList")
    public @ResponseBody  PageData getDesignList(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getDesignList(req) ;
        return res;
    }
    
    //获取楼层列表
    @RequestMapping(method = RequestMethod.POST, value = "/getLevelList")
    public @ResponseBody  PageData getLevelList(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getLevelList(req) ;
        return res;
    }
    
    //获取房间列表
    @RequestMapping(method = RequestMethod.POST, value = "/getRoomList")
    public @ResponseBody  PageData getRoomList(HttpServletRequest request,
                                                  HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getRoomList(req) ;
        return res;
    }
    
    //商机流失
    @RequestMapping(method = RequestMethod.POST, value = "/chageOpportunityStage")
    public @ResponseBody  PageData chageOpportunityStage(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        Map<String, Object> param = req.getReqData();
        String opportunityId = (String)param.get("opportunityId");
        res = service.chageOpportunityStage(opportunityId,"04") ;
        return res;
    }
}
